package com.zwface.demo.function.login.mvvm;


import com.blankj.utilcode.util.ActivityUtils;
import com.zwface.demo.App;
import com.zwface.demo.base.BaseViewModel;
import com.zwface.demo.comm.comm;
import com.zwface.demo.function.login.LoginActivity;
import com.zwface.demo.function.login.RegisterActivity;
import com.zwface.demo.function.login.event.RegisterEvent;
import com.zwface.demo.function.main.MainActivity;
import com.zwface.demo.greendao.UserInfoManager;
import com.zwface.demo.http.HttpCallback;
import com.zwface.demo.http.ZWAsyncHttpClient;
import com.zwface.demo.model.db.UserInfo;
import com.zwface.demo.utils.FastjsonHelper;
import com.zwface.demo.utils.SharedPreferenceUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;

/**
 * created by 张中文
 * on 2020/2/18
 * 描述：
 */
public class LoginViewModel extends BaseViewModel
{
    private static final String TAG = LoginViewModel.class.getSimpleName();

    public ObservableField<String> password = new ObservableField<String>();
    public ObservableField<String> passwordConsirm = new ObservableField<String>();
    public ObservableField<String> name = new ObservableField<String>();
    public ObservableField<String> stuNo = new ObservableField<String>();
    public ObservableInt type = new ObservableInt();


    /**
     * 注册
     */
    public void register()
    {
        if (isDoubleClick("register")) {
            return;
        }
        String pwd = password.get();
        String pwdConfirm = passwordConsirm.get();
        String stuName = name.get();
        String stuNumber = stuNo.get();
        if(!comm.ValidationForm(stuName,"学生姓名不能为空"))return;
        if(type.get() == comm.USER_TYPE_STU)
        if(!comm.ValidationForm(stuNumber,"学号/工号不能为空"))return;
        if(!comm.ValidationForm(pwd,"密码不能为空"))return;
        if(!comm.ValidationForm(pwdConfirm,"确认密码不能为空"))return;
        if(!pwd.equals(pwdConfirm)){
            showToast("两次密码不一致");
            return;
        }

        UserInfo userDb1 = UserInfoManager.queryUserByNo(stuNumber);
        if(userDb1 != null){
            showToast("学号/工号已注册");
            return;
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setName(stuName);
        userInfo.setStuNo(stuNumber);
        userInfo.setType(type.get());
        userInfo.setPassword(pwd);
        showLoading("请稍后...");
        if(UserInfoManager.addUser(userInfo) > 0){
            dismissLoading();
            showToast("注册成功");
            ActivityUtils.finishActivity(RegisterActivity.class);
        }
    }

    /**
     * 登陆
     */
    public void login()
    {
        String stuNumber = stuNo.get();
        String pwd = password.get();
        if(!comm.ValidationForm(stuNumber,"学号不能为空"))return;
        if(!comm.ValidationForm(pwd,"密码不能为空"))return;

        UserInfo userInfo = UserInfoManager.queryUserByNo(stuNumber);
        if(userInfo == null){
            showToast("查无用户，请先注册");
            return;
        }
        if(!userInfo.getPassword().equals(pwd)){
            showToast("密码不正确");
            return;
        }

        SharedPreferenceUtils.setParam(App.getInstance().getApplicationContext(), SharedPreferenceUtils.USER_ID,userInfo.getId()+"");
        SharedPreferenceUtils.setParam(App.getInstance().getApplicationContext(), SharedPreferenceUtils.USER_NAME,userInfo.getName()+"");
        SharedPreferenceUtils.setParam(App.getInstance().getApplicationContext(), SharedPreferenceUtils.USER_STUNO,userInfo.getStuNo()+"");
        SharedPreferenceUtils.setParam(App.getInstance().getApplicationContext(), SharedPreferenceUtils.USER_FACE,userInfo.getFace()+"");
        SharedPreferenceUtils.setParam(App.getInstance().getApplicationContext(), SharedPreferenceUtils.USER_TYPE,userInfo.getType()+"");
        ActivityUtils.startActivity(MainActivity.class);
        ActivityUtils.finishActivity(LoginActivity.class);
    }


}
